package com.xiaotu.spider.mtime.pageprocessor;

import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.TVRecordModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.APageProcessor;

/**
 * 爬取时光网tv相关信息类
 */
@Component("MTimePageProcessor")
@Scope("prototype")
public class MTimePageProcessor extends APageProcessor
{
	
	@Override
	protected Request getStartRequest(JSONObject target)
	{
		String tvName = KeywordEncode.encode(target.getString("tvName"));
		return new Request(
				"http://service.channel.mtime.com/Search.api?Ajax_CallBack=true&"
						+ "Ajax_CallBackType=Mtime.Channel.Services&Ajax_CallBackMethod=GetSearchResult"
						+ "&Ajax_CrossDomain=1&Ajax_RequestUrl=http://search.mtime.com/search/?q="
						+ tvName
						+ "&t=0&t=201733123579432&Ajax_CallBackArgument0="
						+ tvName
						+ "&Ajax_CallBackArgument1=0&Ajax_CallBackArgument2=973"
						+ "&Ajax_CallBackArgument3=0&Ajax_CallBackArgument4=1");
	}
	
	@Override
	public void process(Page page)
	{
		
		String searchResult = page.getRawText();
		if (StringUtils.isEmpty(searchResult))
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		
		searchResult = searchResult.substring(searchResult.indexOf("{"),
				searchResult.indexOf(";var getSearchResult"));
		
		JSONObject searchJSONObject = JSONObject.fromObject(searchResult);
		
		JSONObject valueJSONObject = searchJSONObject.getJSONObject("value");
		
		JSONObject movieResult = valueJSONObject.getJSONObject("movieResult");
		
		if (StringUtil.isEmpty(movieResult) || movieResult.isEmpty())
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		
		JSONArray array = movieResult.getJSONArray("moreMovies");
		
		if (array == null || array.isEmpty())
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		
		Map<String, Object> tvMap = page.getRequest().getExtras();
		
		for (int i = 0; i < array.size(); i++)
		{
			JSONObject item = array.getJSONObject(i);
			String[] movieTitleStrings = item.getString("movieTitle")
					.split(SepratorUtil.SEP_SPACE_EN);
			String title = movieTitleStrings[0];
			String releasetime = RegexUtils.regexSearch("\\(\\d{4}\\)",
					item.getString("movieTitle"));
			if (StringUtils.isEmpty(releasetime))
				continue;
			releasetime = RegexUtils.replaceStr(releasetime, "\\(|\\)",
					StringUtils.EMPTY);
			
			if (title.equals(tvMap.get("tvName")) && item.has("movieRating"))
			{
				page.putField(TVRecordModel.class.getName(),
						new TVRecordModel(tvMap.get("tvid") + "",
								tvMap.get("tvName") + "",
								Constants.SiteName.MITIME,
								item.getString("movieRating"), null, null, null,
								null, null,null));
				return;
				
				// String tvTime = tvMap.get("releasetime") == null ? null
				// : ((tvMap.get("releasetime") + "").length() > 3
				// ? (tvMap.get("releasetime") + "").substring(0,
				// 4)
				// : null);
				// if (releasetime.equals(tvTime))
				// {
				// page.putField(TVRecordModel.class.getName(),
				// new TVRecordModel(tvMap.get("tvid") + "",
				// tvMap.get("tvName") + "",
				// Constants.SiteName.MITIME,
				// item.getString("movieRating"), null, null,
				// null, null, null));
				// return;
				// }
			}
		}
		throw new SpiderException(SpiderExceptionCode.DataInvalideException);
	}
	
	@Override
	protected String getSpiderType()
	{
		return Constants.SpiderTaskType.TV_Mtime;
	}
	
	@Override
	public String getPageDataType(Page page)
	{
		return Constants.PageDataType.TV_RECORD;
	}
	
}